package edu.bu.CS893.data;

import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class FacebookResponseHandler extends DefaultHandler {
	private FacebookData _fbData = new FacebookData();
	private StringBuilder _builder = new StringBuilder();
	private String _currentTag;
	private String _currentKey;

	private static String ERROR_RESPONSE = "error_response";
	private static String ERROR_CODE = "error_code";
	private static String ERROR_MSG = "error_msg";
	private static String REQUEST_ARGS = "request_args";
	private static String AUTH_TOKEN_RESPONSE = "auth_createToken_response";
	private static String KEY = "key";
	private static String VALUE = "value";
	private static String AUTH_SESSION_RESPONSE = "auth_getSession_response";
	private static String SESSION_KEY = "session_key";
	private static String UID = "uid";
	private static String EXPIRES  = "expires";
	private static String SECRET = "secret";
	private static String MESSAGE = "message";
	private static String USER = "user";
	private static String NAME = "name";
	private static String PIC = "pic_square";
	private FacebookUser _current_user;

	public FacebookData getFacebookData() {
		return _fbData;
	}

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		_currentTag = localName;

		if (_currentTag.equals(ERROR_RESPONSE)) {
		}
		else if (_currentTag.equals(REQUEST_ARGS))
			_fbData.request_args = new HashMap<String, String>();
		else if (_currentTag.equals(AUTH_SESSION_RESPONSE))
			_fbData.session = new FacebookSession();
		else if (_currentTag.equals(USER))
			_current_user = new FacebookUser();

		return;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		_builder.append(ch, start, length);
	};

	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals(ERROR_CODE))
			_fbData.error_code = _builder.toString().trim();
		else if (localName.equals(ERROR_MSG))
			_fbData.error_msg = _builder.toString().trim();
		else if (localName.equals(AUTH_TOKEN_RESPONSE))
			_fbData.auth_token = _builder.toString().trim();
		else if (localName.equals(KEY))
			_currentKey = _builder.toString().trim();
		else if (localName.equals(VALUE))
			_fbData.request_args.put(_currentKey, _builder.toString().trim());
		else if (localName.equals(SESSION_KEY))
			_fbData.session.session_key = _builder.toString().trim();
		else if (localName.equals(UID)) {
			if (_current_user != null)
				_current_user.uid = Long.parseLong( _builder.toString().trim() );
			else if (_fbData.session != null)
				_fbData.session.uid = _builder.toString().trim();
		}
		else if (localName.equals(EXPIRES))
			_fbData.session.expires = _builder.toString().trim();
		else if (localName.equals(SECRET))
			_fbData.session.secret = _builder.toString().trim();
		else if (localName.equals(MESSAGE))
			_current_user.status = _builder.toString().trim();
		else if (localName.equals(USER)) {
			if (_fbData.users == null)
				_fbData.users = new ArrayList<FacebookUser>();
			_fbData.users.add(_current_user);
			_current_user = null;
		}
		else if (localName.equals(NAME))
			_current_user.name = _builder.toString().trim();
		else if (localName.equals(PIC)) {
			_current_user.pic = _builder.toString().trim();
		}
		
		_builder = new StringBuilder();
	}

}
